class PostsController < ApplicationController

	before_filter :login_required, :only=>[ :new, :create, :edit, :update, :destroy, :reply]
	before_filter :users_post, :only=>[:edit, :update, :destroy]

  before_filter :post_level_check, :only=>[:new]
  before_filter :reply_level_check, :only=>[:reply]
  before_filter :view_level_check, :only=>[:show]

  def show_post_body
    if !params[:id].nil?
      @post = Post.find_by_id(params[:id])
      render :partial => "posts/show_post_body",  :locals => { :post => @post }
    end
  end

	def show
		begin
			@post = Post.find(params[:id])
			if !@post.parent_id.nil?
				redirect_to show_post_path(@post.parent_id)
			else
				@posts = @post.find_children()
        @posts.sort! { |a,b|
          a.created_at <=> b.created_at
        }
			end
		rescue
			render :file=>"public/404.html", :status => 404
		end
	end

  def new
    @post = Post.new
    session[:category_id] = params[:cat]
  end

	def create
		@post = Post.new(params[:post])
		@post.user_id = session[:user_id]
		if !@post.parent_id.nil?
			parent_post = Post.find_by_id(@post.parent_id)
			parent_post.updated_at = DateTime.now()
			parent_post.last_reply_id = @post.user_id
			parent_post.save
		end
		if @post.save
      session[:category_id] = nil
      @post.prepare_body
      
      if !@post.parent_id.nil?
        redirect_to show_post_path(@post.parent_id)
			elsif !(session[:ret_post].nil?)
				redirect_to session[:ret_post]
			else
				redirect_to show_post_path(@post.id)
			end
		else
      if !@post.parent_id.nil?
        render :template => "posts/reply"
      else
        render :template => "posts/new"
      end
		end
	end

  def edit
    @post = Post.find(params[:id])
    @post.strip_edit_info
    @categories = Category.find(:all)
  end

  def update
    @post = Post.find(params[:id])
    if @post.update_attributes(params[:post])
      @post.prepare_body
      @post.add_edit_info(session[:user_id])
      @post.update_children_category
      redirect_to show_post_path(@post.id)
    else
      render :template => "posts/edit"
    end
  end

  def destroy
    @post = Post.find_by_id(params[:id])
    cat_id = @post.category_id
    parent_id = @post.parent_id
    @post.destroy
    if !(session[:ret_post].nil?)
      session[:ret_post] = nil
    end
    if parent_id.nil?
      redirect_to show_category_posts_path(cat_id)
    else
      redirect_to show_post_path(parent_id)
    end
  end
 
	def reply
		@post = Post.new
    if !params[:parent_id].nil?
      session[:parent_post] = params[:parent_id]
    end
		@parent = Post.find_by_id(session[:parent_post])
	end

  def denied
    @post = Post.find(params[:id])
  end

  private
	def users_post
		@post = Post.find_by_id(params[:id])
		if !@post.nil? && !session[:user_id].nil?
			return true if ( @post.belongs_to_user?(session[:user_id]) || User.is_admin(session[:user_id]) )
		end
		redirect_to post_denied_path(session[:post_id])
	end


  def view_level_check
    return true if User.get_user_level_id(session[:user_id]) <= Post.view_level_id(params[:id])
    session[:denied_path] = request.request_uri
    redirect_to denied_path
  end


end